﻿
Imports Microsoft.Office.Interop.Outlook

Namespace UI
    Public Class pmMoveMailForm
        Inherits PMOutlookAddIn.UI.pmBaseFolderTreeMailForm

        Private _dpnummer As String = String.Empty

        Public Sub New(ByVal App As Application, ByVal mailToWorkWith As MailItem)
            MyBase.New(App, mailToWorkWith)
            InitializeComponent()
        End Sub

        Private Sub pmMoveMailForm_ActionButtonClicked() Handles Me.ActionButtonClicked
            Dim destinationFolder As Outlook.MAPIFolder = _pmFoldertree.GetSelectedFolder()

            If IsNothing(destinationFolder) Then
                Return
            Else
                'als destinationfolder geen dpnummer bevat dan moet de naam ervan aangepast worden voor verdere afhandeling van nieuwe mails
                If Not destinationFolder.Name.Contains(_dpnummer) Then
                    destinationFolder.Name = _dpnummer + " - " + destinationFolder.Name
                End If
            End If

            Dim mailMover As IMailManager = New MailManager(_app, _mailToWorkWith)
            mailMover.MoveTo(destinationFolder)
            SharedMethods.SetCurrentFolder(_app, destinationFolder)
            Me.Close()
        End Sub

        Public Overrides Function getSearchString() As String
            'zoeken naar dpnummer²
            _dpnummer = SharedFunctions.GetDPNumberOutOfString(_mailToWorkWith.Subject)
            If _dpnummer <> String.Empty Then
                Return _dpnummer
            End If

            'zoeken op eerste contact van de lijst van ontvangers
            If _mailToWorkWith.Recipients.Count > 0 Then
                Dim SenderEmail As String = _mailToWorkWith.SenderEmailAddress

                Dim fullname As String = SharedFunctions.contactFullnameByEmail(_app, SenderEmail)
                If fullname <> String.Empty Then
                    Return fullname
                End If
            End If

            Return String.Empty
        End Function

        Public Overrides Function getEmail() As String
            Return _mailToWorkWith.SenderEmailAddress
        End Function

        Public Overrides Sub pmForm_Shown(ByVal sender As Object, ByVal e As System.EventArgs)
            If _pmFoldertree.TreeView1.Nodes(0) Is _pmFoldertree.TreeView1.SelectedNode Then
                SharedMethods.ControlSetFocus(txtSearchBox)
            Else
                SharedMethods.ControlSetFocus(btnDoAction)
            End If

        End Sub

    End Class
End Namespace